package com.lhkj.ct.base.utils;

import com.lhkj.ct.base.enums.ReturnStatus;
import com.lhkj.ct.base.exception.BusinessException;
import org.apache.commons.lang3.StringUtils;

import java.util.function.Consumer;

/**
 * @Description: 断言
 * @Author:
 * @Date: 2020/6/5 15:48
 */

public class Asserts {

    /**
     * 断言为真
     * @param condition 条件
     * @param failConsumer 断言为假的校验
     * @param params 回调入参
     * @param <T> 入参类型
     */
    public static <T> void assertTrue(boolean condition, Consumer<T> failConsumer, T params) {
        if (!condition) {
            failConsumer.accept(params);
        }
    }

    /**
     * 断言为真
     * @param condition 条件
     * @param errMsg 异常信息
     */
    public static void assertTrue(boolean condition, String errMsg) {
        if (!condition) {
            throw new BusinessException(ReturnStatus.LOGICAL_VALIDATE_FAILED, errMsg);
        }
    }

    /**
     * 断言为假
     * @param condition 条件
     * @param errMsg 异常信息
     */
    public static void assertFalse(boolean condition, String errMsg) {
        if (condition) {
            throw new BusinessException(ReturnStatus.LOGICAL_VALIDATE_FAILED, errMsg);
        }
    }

    /**
     * 断言不为null
     * @param data 数据
     * @param errMsg 异常信息
     */
    public static void assertNotNull(Object data, String errMsg) {
        if (data == null) {
            throw new BusinessException(ReturnStatus.LOGICAL_VALIDATE_FAILED, errMsg);
        }
    }

    /**
     * 断言为null
     * @param data 数据
     * @param errMsg 异常信息
     */
    public static void assertNull(Object data, String errMsg) {
        if (data != null) {
            throw new BusinessException(ReturnStatus.LOGICAL_VALIDATE_FAILED, errMsg);
        }
    }

    /**
     * 断言不为空字符串
     * @param data 字符串
     * @param errMsg 异常信息
     */
    public static void assertNotEmpty(String data, String errMsg) {
        if (StringUtils.isEmpty(data)) {
            throw new BusinessException(ReturnStatus.LOGICAL_VALIDATE_FAILED, errMsg);
        }
    }

    /**
     * 断言为空字符串
     * @param data 字符串
     * @param errMsg 异常信息
     */
    public static void assertEmpty(String data, String errMsg) {
        if (StringUtils.isNotEmpty(data)) {
            throw new BusinessException(ReturnStatus.LOGICAL_VALIDATE_FAILED, errMsg);
        }
    }

    /**
     * 断言字符串相等
     * @param expected 字符串
     * @param actual 字符串
     * @param errMsg 异常信息
     */
    public static void assertEquals(String expected, String actual, String errMsg) {
        if (!StringUtils.equals(expected, actual)) {
            throw new BusinessException(ReturnStatus.LOGICAL_VALIDATE_FAILED, errMsg);
        }
    }

    /**
     * 断言字符串不相等
     * @param expected 字符串
     * @param actual 字符串
     * @param errMsg 异常信息
     */
    public static void assertNotEquals(String expected, String actual, String errMsg) {
        if (StringUtils.equals(expected, actual)) {
            throw new BusinessException(ReturnStatus.LOGICAL_VALIDATE_FAILED, errMsg);
        }
    }
}
